package com.controller;

import com.entity.*;
import com.service.*;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GaiqianshenqingController
 * @Description 改签申请模块控制层
 */
@Controller
public class GaiqianshenqingController extends BaseController{

    @Autowired
    private GaiqianshenqingService gaiqianshenqingService;

    @Autowired
    private UsersService usersService;



    //前台-改签申请列表页
    @RequestMapping("/gaiqianshenqingManaQian")
    public ModelAndView gaiqianshenqingManaQian(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("gaiqianshenqingManaQian");
        mav.addObject("actionName","gaiqianshenqingManaQian");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-改签申请图片列表页
    @RequestMapping("/gaiqianshenqingManaQiantp")
    public ModelAndView gaiqianshenqingManaQiantp(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("gaiqianshenqingManaQiantp");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-改签申请管理列表页
    @RequestMapping("/gaiqianshenqingMana")
    public ModelAndView gaiqianshenqingMana(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("gaiqianshenqingMana");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-改签申请查看列表页
    @RequestMapping("/gaiqianshenqingChakan")
    public ModelAndView gaiqianshenqingChakan(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("gaiqianshenqingChakan");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-改签申请按id列排序列表页
    @RequestMapping("/gaiqianshenqingManaQianSort")
    public ModelAndView gaiqianshenqingManaQianSort(Gaiqianshenqing  req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo) {
        ModelAndView mav = new ModelAndView("gaiqianshenqingManaQian");
        mav.addObject("actionName","gaiqianshenqingManaQianSort");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-改签申请详情页
    @GetMapping("/gaiqianshenqingDetailQian/{id}")
    @ResponseBody
    public ModelAndView gaiqianshenqingDetailQian(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("gaiqianshenqingDetailQian");
        Gaiqianshenqing info= gaiqianshenqingService.findById(id);
        mav.addObject("gaiqianshenqing", info);

        return mav;
    }

    //后台-改签申请详情页
    @GetMapping("/gaiqianshenqingDetail/{id}")
    @ResponseBody
    public ModelAndView gaiqianshenqingDetail(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("gaiqianshenqingDetail");
        Gaiqianshenqing info= gaiqianshenqingService.findById(id);
        mav.addObject("gaiqianshenqing", info);
        return mav;
    }

    //获取所有改签申请数据接口
    @RequestMapping("/gaiqianshenqingAll")
    @ResponseBody
    public ApiResponse<Gaiqianshenqing> gaiqianshenqingAll(){
        List<Gaiqianshenqing> gaiqianshenqingList=gaiqianshenqingService.findAll();
        return ApiResponse.success(gaiqianshenqingList);
    }

    //根据条件（字符类型模糊匹配查询）获取改签申请数据接口
    @RequestMapping("/gaiqianshenqingSearch")
    @ResponseBody
    public ApiResponse<Gaiqianshenqing> gaiqianshenqingSearch(Gaiqianshenqing req){
        List<Gaiqianshenqing> gaiqianshenqingList=gaiqianshenqingService.find(req);
        return ApiResponse.success(gaiqianshenqingList);
    }

    //根据条件（字符类型完全匹配查询）获取改签申请数据接口
    @RequestMapping("/gaiqianshenqingSearchByEqualTo")
    @ResponseBody
    public ApiResponse<Gaiqianshenqing> gaiqianshenqingSearchByEqualTo(Gaiqianshenqing req){
        List<Gaiqianshenqing> gaiqianshenqingList=gaiqianshenqingService.findByEqualTo(req);
        return ApiResponse.success(gaiqianshenqingList);
    }

    //改签申请添加或修改接口
    @PostMapping("/gaiqianshenqingAddOrUpdate")
    @ResponseBody
    public ApiResponse<Integer> gaiqianshenqingAddOrUpdate(@RequestBody Gaiqianshenqing req, HttpSession session, Boolean isUPdateSession){



        int result = gaiqianshenqingService.saveOrUpdate(req);


        return ApiResponse.success(result);
    }

    //改签申请修改单独一个字段接口
    @PostMapping("/gaiqianshenqingSet")
    @ResponseBody
    public ApiResponse<Integer> gaiqianshenqingSet(Gaiqianshenqing req){
        int result = gaiqianshenqingService.saveOrUpdate(req);
        return ApiResponse.success(result);
    }



    //改签申请删除接口
    @RequestMapping("/gaiqianshenqingDel/{id}")
    @ResponseBody
    public ApiResponse<Integer> gaiqianshenqingDel(@PathVariable Integer id){
        int result = gaiqianshenqingService.deleteById(id);
        return ApiResponse.success(result);
    }

    //后台-改签申请修改页
    @RequestMapping("/gaiqianshenqingGet/{id}")
    @ResponseBody
    public ModelAndView gaiqianshenqingGet(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("gaiqianshenqingSet");
        Gaiqianshenqing info = gaiqianshenqingService.findById(id);
        mav.addObject("gaiqianshenqing", info);
        return mav;
    }



    //后台-我的改签申请查看页面
    @RequestMapping("/gaiqianshenqingChakanMy")
    public ModelAndView gaiqianshenqingChakanMy(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("gaiqianshenqingChakanMy");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }

    //后台-我的改签申请管理列表页面
    @RequestMapping("/gaiqianshenqingManaMy")
    public ModelAndView gaiqianshenqingManaMy(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("gaiqianshenqingManaMy");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }

    //前台-我的改签申请管理列表页面
    @RequestMapping("/gaiqianshenqingManaMyQian")
    public ModelAndView gaiqianshenqingManaMyQian(Gaiqianshenqing req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("gaiqianshenqingManaMyQian");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }


    //前台-用户添加改签申请页面
    @GetMapping("/usersgaiqianshenqingAddQian/{usersid}")
    @ResponseBody
    public ModelAndView usersgaiqianshenqingAddQian(@PathVariable Integer usersid){
        ModelAndView mav = getUsersGaiqianshenqingAddModelAndView(usersid,"usersgaiqianshenqingAddQian");
        return mav;
    }

    //后台-用户添加改签申请页面
    @GetMapping("/usersgaiqianshenqingAdd/{usersid}")
    @ResponseBody
    public ModelAndView usersgaiqianshenqingAdd(@PathVariable Integer usersid){
        ModelAndView mav = getUsersGaiqianshenqingAddModelAndView(usersid,"usersgaiqianshenqingAdd");
        return mav;
    }

    //后台-用户改签申请查看页面
    @RequestMapping("/usersgaiqianshenqingGetByusersid/{usersid}")
    @ResponseBody
    public ModelAndView usersgaiqianshenqingGetByusersid(Gaiqianshenqing req, @PathVariable Integer usersid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("usersgaiqianshenqingChakan");
        req.setUsersid(usersid);
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-用户改签申请查看页面
    @RequestMapping("/usersgaiqianshenqingGetByusersidQian/{usersid}")
    @ResponseBody
    public ModelAndView usersgaiqianshenqingGetByusersidQian(Gaiqianshenqing req, @PathVariable Integer usersid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("gaiqianshenqingManaQian");
            mav.addObject("actionName","gaiqianshenqingManaQian");
        req.setUsersid(usersid);
        return getModelAndView(req, pageNo, mav, "id");
    }


    //用户改签申请数据封装
    private ModelAndView getUsersGaiqianshenqingAddModelAndView(Integer usersid, String viewName) {
        ModelAndView mav = new ModelAndView(viewName);
        Users users=usersService.findById(usersid);
        mav.addObject("users", users);

        return mav;
    }



    //改签申请模糊匹配查询分页数据封装
    private ModelAndView getModelAndView(Gaiqianshenqing req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Gaiqianshenqing> pageWrap=new PageWrap<Gaiqianshenqing>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Gaiqianshenqing> pageInfo= gaiqianshenqingService.findPage(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
    }

    //改签申请完全匹配查询分页数据封装
    private ModelAndView getModelAndViewByEqualTo(Gaiqianshenqing req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Gaiqianshenqing> pageWrap=new PageWrap<Gaiqianshenqing>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Gaiqianshenqing> pageInfo= gaiqianshenqingService.findPageByEqualTo(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
        }
}
